package Hander.Add;

import View.Add.AddAchievementView;
import View.LeaderView;
import entity.Achievement;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddAchievementViewHander implements ActionListener {
    private AddAchievementView addAchievementView;
    private LeaderView leaderView;
    public AddAchievementViewHander(AddAchievementView addAchievementView, LeaderView leaderView) {
        this.addAchievementView = addAchievementView;
        this.leaderView=leaderView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("添加".equals(text)){
            PersonnelService personnelService=new PersonnelServiceImpl();
            Achievement achievement= addAchievementView.buildAchievement();
            Boolean addresult=personnelService.add(achievement.getAllattributes(), addAchievementView.getTablesName());
            if(addresult){
                leaderView.reloadTable(addAchievementView.getTablesName());
                JOptionPane.showMessageDialog(addAchievementView,"添加成功");
                addAchievementView.dispose();
            }else if(achievement.getGrade()>4||achievement.getGrade()<0){
                JOptionPane.showMessageDialog(addAchievementView,"Grade取值异常！");
            }
            else{
                System.out.println("添加错误！");
                JOptionPane.showMessageDialog(addAchievementView,"添加失败");
            }
        }
    }
}
